package core

import (
	"fmt"
	"go-backend-simple-scaffold/core/internal"
	"go-backend-simple-scaffold/global"
	"go-backend-simple-scaffold/utils"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
)

// Zap 获取 zap.Logger
func Zap() (logger *zap.Logger) {
	// 判断目录是否存在
	if ok, _ := utils.PathExists(global.GBSS_CONFIG.Zap.Director); !ok {
		fmt.Printf("create %v directory\n", global.GBSS_CONFIG.Zap.Director)
		// 创建目录，设置权限
		_ = os.Mkdir(global.GBSS_CONFIG.Zap.Director, os.ModePerm)
	}
	// 获取日志级别
	levels := global.GBSS_CONFIG.Zap.Levels()
	length := len(levels)
	cores := make([]zapcore.Core, 0, length)
	// 创建不同日志级别的核心
	for i := 0; i < length; i++ {
		core := internal.NewZapCore(levels[i])
		cores = append(cores, core)
	}
	// 用于创建一个日志记录器，该记录器将日志消息同时写入多个输出目标（也称为核心或 Core）
	logger = zap.New(zapcore.NewTee(cores...))
	if global.GBSS_CONFIG.Zap.ShowLine {
		// 配置调用者信息：行号和函数名
		logger = logger.WithOptions(zap.AddCaller())
	}
	return logger
}
